{include file='head'/}
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <table id="database_list" lay-filter="database_list"></table>
    </div>
</div>
<script type="text/html" id="add_toolbar">
    <div class="layui-btn-group demoTable">
        <a class="layui-btn layui-btn-sm layui-btn-danger" data-open-full="{:__url('system/database/table_create')}" data-title="创建表">
            <i class="layui-icon layui-icon-add-1"></i> 创建表</a>
        <a class="layui-btn layui-btn-sm"
           data-type="beifenData">
            <i class="fa fa-plus"></i> 立即备份</a>
        <a class="layui-btn layui-btn-normal layui-btn-sm"
           data-type="youhuaData">优化表</a>
        <a class="layui-btn layui-btn-zise layui-btn-sm"
           data-type="xiufuData">修复表</a>
        <a class="layui-btn layui-btn-lvse layui-btn-sm"
           data-type="huanyuanData"
           data-open="{:__url('system/database/databack_list')}" data-title="备份列表">数据还原</a>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm"
       lay-event="youhua">优化</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm"
       lay-event="xiufu">修复</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm"
       data-open-full="{:__url('system/database/table_edit')}?table={{d.Name}}" data-title="编辑">编辑</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-lvse"
       data-open="{:__url('system/database/table_crud')}?table={{d.Name}}" data-title="一键CURD" data-reload="1">一键CURD</a>
</script>

{include file='footer'/}
<script>
  layui.use(['table', 'form', 'jquery'], function () {
    var table = layui.table, $ = layui.jquery, form = layui.form;
    //第一个实例
    table.render({
      elem: '#database_list'
      , toolbar: '#add_toolbar'
      , defaultToolbar: [{
        layEvent: 'refresh',
        icon: 'layui-icon-refresh',
        title: '刷新'
      }, 'filter', 'exports']
      , method: 'get'
      , url: "{:__url('system/database/index')}"
      , parseData: function (res) {
        return {
          "code": res.code_lay,
          "msg": res.msg,
          "count": res.count,
          "data": res.data
        };
      }
      , done: function (res) { //res 即为原始返回的数据
        $("#total").html(res.count);
      }
      , cols: [[ //表头
        {type: 'checkbox', LAY_CHECKED: true}
        , {field: 'Name', title: '表名', minWidth: 140}
        , {field: 'Comment', title: '备注', minWidth: 140}
        , {field: 'Engine', title: '类型',width: 100}
        , {field: 'Collation', title: '编码',minWidth: 150}
        , {field: 'Rows', title: '数据量', width: 90,align: 'center'}
        , {field: 'Data_length', title: '数据大小', width: 110}
        , {field: 'Create_time', title: '创建时间'}
        , {field: 'Data_free', title: '碎片', width: 80 ,align: 'center'}
        , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 310, align: 'center'}
      ]]
      , id: 'Datalist'
      , page: false
      , limit: 10000
    });
    //监听行工具事件
    table.on('tool(database_list)', function (obj) {
      var data = obj.data;
      if (obj.event === 'youhua') {
        getevent("{:__url('system/database/optimize')}", data);
      } else if (obj.event === 'xiufu') {
        getevent("{:__url('system/database/repair')}", data);
      }
    });

    var $ = layui.$, active = {
      //优化表数据
      youhuaData: function () {
        var tables = table_get_ids(table, 'Datalist', 'Name');
        if (tables === false) {
          return false;
        }
        var loading = layer.load();
        $.post("{:__url('system/database/optimize')}", {
          table: tables
        }, function (res) {
          layer.close(loading);
          layer.msg(res.msg, {
            icon: res.icon
          })
        })
      }
      //修复表数据
      , xiufuData: function () {
        var tables = table_get_ids(table, 'Datalist', 'Name');
        if (tables === false) {
          return false;
        }
        var loading = layer.load();
        $.post("{:__url('system/database/repair')}", {
          table: tables
        }, function (res) {
          layer.close(loading);
          layer.msg(res.msg, {
            icon: res.icon
          })
        })
      }
      //立即备份
      , beifenData: function () {
        var loading = layer.load();
        $.post("{:__url('system/database/backup')}", function (res) {
          layer.close(loading);
          if (res.code == 200) {
              HuiAdminShow("数据备份列表", "{:__url('system/database/databack_list')}");
          } else {
            hui_msg(res.msg, 2);
          }

        });
      }
    };
    $('.demoTable .layui-btn').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
    //监听行工具事件
    table.on('toolbar(database_list)', function (obj) {
      if (obj.event === 'refresh') {
        table.reload('Datalist');
      }
    });

    function getevent(url, data) {
      $.get(url, {
        table: data.Name
      }, function (res) {
        layer.msg(res.msg, {
          icon: res.icon
        })
      })
    }

    // 刷新表格数据
    window.refreshTable = function(param) {
      table.reload('Datalist');
    }

  });
</script>
</body>
</html>
